package cn.wolfcode.p2p.base.service.impl;

import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.MailVerify;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.exception.DisplayableException;
import cn.wolfcode.p2p.base.mapper.UserInfoMapper;
import cn.wolfcode.p2p.base.service.IMailVarifyService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.util.BitStateUtil;
import cn.wolfcode.p2p.base.util.Constants;
import cn.wolfcode.p2p.base.util.DateUtil;
import cn.wolfcode.p2p.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private IMailVarifyService mailVarifyService;

    @Override
    public int save(UserInfo userInfo) {
        return userInfoMapper.insert(userInfo);
    }

    @Override
    public int update(UserInfo userInfo) {
        int count = userInfoMapper.updateByPrimaryKey(userInfo);
        if(count == 0){
            throw new RuntimeException("乐观锁异常,userInfoId: " + userInfo.getId());
        }
        return count;
    }

    @Override
    public UserInfo get(Long id) {
        return userInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public void basicInfoSave(UserInfo userInfo) {
        //获取当前用户
        UserInfo current = this.getCurrent();
        //设置其current信息
        current.setEducationBackground(userInfo.getEducationBackground());
        current.setHouseCondition(userInfo.getHouseCondition());
        current.setIncomeGrade(userInfo.getIncomeGrade());
        current.setKidCount(userInfo.getKidCount());
        current.setMarriage(userInfo.getMarriage());
        //设置其状态码 判断其当前对象有没有已经存在了基本信息验证的验证码了
        if(!current.isHasBasicInfo()){
            //如果没有 帮其添加验证码
            current.setBitState(BitStateUtil.HAS_BASIC_INFO);
        }
        //直接更新到数据库中
        update(current);
    }


    @Override
    public UserInfo getCurrent() {
        return userInfoMapper.selectByPrimaryKey(UserContext.getCurrent().getId());
    }

    @Override
    public void bindEmail(String key) {
        MailVerify mailVerify = mailVarifyService.selectByUUID(key);
        //判断是否有 而且是在有效期之内
        if(mailVerify == null){
            throw new RuntimeException("验证地址有误,请重新输入正确的验证地址!");
        }
        if(DateUtil.getTimeBetwwen(mailVerify.getSendTime(),new Date()) > Constants.DEFAULT_TIME_DAY * 24 * 60 * 60){
            throw new RuntimeException("验证邮箱地址已超过有效期,请重新验证!");
        }
        //判断当前用户是否已经绑定了邮箱
        UserInfo userInfo = this.get(mailVerify.getUserId());
        if(userInfo.getHasBindEmail()){
            throw new RuntimeException("您已绑定邮箱,请勿重复绑定");
        }
        //用户可以绑定当前邮箱 改变用户的邮箱状态码
        userInfo.addState(BitStateUtil.OP_BIND_EMAIL);
        userInfo.setEmail(mailVerify.getEmail());
        //更新到数据库
        userInfoMapper.updateByPrimaryKey(userInfo);
    }
}
